<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/8/31
 * Time: 下午4:07
 */

namespace App\Library\Mall;


use App\Models\Order;
use App\WeChat\WxApi\WxTemplateMessageApi;
use App\WxMessage\NewOrderMessage;

class WxMsgClient
{
    /**
     * @param Order $order
     * @param null $shop
     * @return bool
     * @throws \Exception
     */
    public static function sendNewOrderMessage(Order $order, $shop = null){
        if (!$shop) {
            $shop = $order->shop;
        }

        $kefu = $shop->kefus()->available()->first();
        if ($kefu) {
            $message = new NewOrderMessage();
            $message->setFirst($shop->shop_name.'商家，您有一笔新订单！');
            $message->setOrderNo($order->order_no);
            $message->setOrderName($order->trade->subject);
            $message->setOrderAmount(formatAmount($order->total_fee));
            $message->setUserInfo($order->buyer_name);
            $message->setTradeType($order->pay_type == 1 ? '在线支付' : '货到付款');
            $message->setTouser($kefu->openid);
            $message->setUrl(url('mobile/sold/detail?order_id='.$order->order_id));

            WxTemplateMessageApi::sendMessage($message);
        }

        return true;
    }
}
